package com.mlz.mlzmall.ui.activity.act.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mlz.mlzmall.R;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

//import com.mlz.mlzmall.view.circleimageview.MainFragment;

public class JoinActivity extends Activity {
    RelativeLayout iv_back_share_FreeTrial;
    Button btn_back_mainA;
    String promotionIdUrl;
    // 显示礼品
    ImageView gift_picture;
    // 显示指纹
    WebView iv_scan_code;
    // 用于下载网路图片、定义参数
    private DisplayImageOptions options;
    // 新建静态实例对象
    private ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join);
        // 设置选项
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.avatar)
                .showImageOnFail(R.drawable.avatar)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();
        //初始化
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplication())
                .memoryCacheExtraOptions(480, 800)  // 缓存在内存的图片的宽和高度
                .discCacheExtraOptions(480, 800, Bitmap.CompressFormat.PNG, 70, null)   //CompressFormat.PNG类型，70质量（0-100）
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(2 * 1024 * 1024)   // 缓存到内存的最大数据
                .discCacheSize(50 * 1024 * 1024)    // 缓存到文件的最大数据
                .discCacheFileCount(1000)           // 文件数量
                .defaultDisplayImageOptions(options)// 上面的options对象，一些属性配置
                .build();
        imageLoader.init(config);
        // 初始化控件
        intView();
        // 显示节目
        joinView();
        // 回退监听
        setListener();

    }

    // 初始化控件
    private void intView() {
        iv_back_share_FreeTrial = (RelativeLayout) findViewById(R.id.iv_back_join);
        btn_back_mainA = (Button) findViewById(R.id.btn_back_mainA);
        promotionIdUrl = JoinActivity.this.getIntent().getStringExtra("promotionIdUrl");
        gift_picture = (ImageView) findViewById(R.id.gift_picture);
    }

    private void setListener() {
        // 返回上一页
        iv_back_share_FreeTrial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.left_in);
            }
        });
        // 返回首页
        btn_back_mainA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(JoinActivity.this, MainActivity.class));
                finish();
            }
        });
    }

    private void joinView() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                    // 构建请求对象
//                    RestTemplate restTemplate = new RestTemplate();
//                    // 从手机存储卡取sessionKey
//                    ACache mCache = ACache.get(getApplicationContext());
//                    String sessionKey = mCache.getAsString("sessionKeyLogin");
//                    // 设置选择请求
//                    HttpHeaders requestHeadSeleted = new HttpHeaders();
//                    // 设置请求头
//                    requestHeadSeleted.set("Cookie", "JSESSIONID=" + sessionKey);
//                    // 把请求头放在请求实体里
//                    requestHeadSeleted.setContentType(new MediaType("application", "json"));
//                    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeadSeleted);
//                    //参加抽奖活动结果URL
//                    String urlJoinResult = "http://app.mlzmall.com/mlzmall/api/promotion/enrollPromotionResult?promotionId=" + String.valueOf(StringTools.promotionId);
//                    final ResponseEntity<joinResult> joinResult = restTemplate.exchange(urlJoinResult, HttpMethod.GET, requestEntity, joinResult.class);
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                        // 显示指纹
//                        try {
//                            if(null != joinResult.getBody().getData()) {
//                                // 显示礼品图片
//                                imageLoader.displayImage(joinResult.getBody().getData().getGiftImage(), gift_picture, options);
//                               /* iv_scan_code.getSettings().setJavaScriptEnabled(true);
//                                iv_scan_code.loadUrl(joinResult.getBody().getData().getQrcodeImage());
//                                iv_scan_code.setWebViewClient(new WebViewClient());*/
//                            }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//            }
//        }).start();
    }

}
